package com.yjhb.android.sbz.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.adapter.CheckViewPagerAdapter;
import com.yjhb.android.sbz.dialog.ImageTextDialog;
import com.yjhb.android.sbz.dialog.TipOffDialog;
import com.yjhb.android.sbz.dialog.TipsDialog;
import com.yjhb.android.sbz.entity.CheckEntity;
import com.yjhb.android.sbz.entity.CheckResult;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.entity.TaskOrder;
import com.yjhb.android.sbz.enums.TaskStatus;
import com.yjhb.android.sbz.mvp.taskcheck.TaskCheckPresenter;
import com.yjhb.android.sbz.mvp.taskcheck.TaskCheckView;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.base.BaseDialog;
import com.jojo.android.zxlib.tool.ZxSpanTool;
import com.jojo.android.zxlib.tool.ZxToast;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/15 10:40
 */
public class TaskCheckActivity extends BaseActivity<TaskCheckPresenter> implements ViewPager.OnPageChangeListener, TaskCheckView {
    private final String TAG = "TaskCheckActivity";

    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.no_btn)
    Button noBtn;
    @BindView(R.id.tips_btn)
    Button tipsBtn;
    @BindView(R.id.pass_btn)
    Button passBtn;
    @BindView(R.id.title_right_tv)
    TextView title_right_tv;

    CheckViewPagerAdapter adapter;
    ArrayList<CheckResult> checkResults;
    int index;


    @Override
    protected TaskCheckPresenter initPresenter() {
        return new TaskCheckPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_task_check;
    }

    @Override
    protected void initUI() {
        Serializable serializable = getIntent().getSerializableExtra("dataSet");
        if (serializable != null) {
            checkResults = (ArrayList<CheckResult>) serializable;
        }
        index = getIntent().getIntExtra("index", 0);
        viewpager.setAdapter(adapter = new CheckViewPagerAdapter(this, checkResults));
        viewpager.setCurrentItem(index);
    }

    @Override
    protected void initData() {
        CheckEntity taskOrder = checkResults.get(index).getTaskOrder();
        presenter.userAllOrder(index, taskOrder.getTaskId(), taskOrder.getUserId(),taskOrder.getId());
    }

    @Override
    protected void regEvent() {
        viewpager.addOnPageChangeListener(this);
    }

    @Override
    public void refresh() {
        CheckResult checkResult = checkResults.get(index);
        switch (TaskStatus.getStatus(checkResult.getTaskOrder().getState())) {
            case TASK_STATUS_SUBMIT:
                tipsBtn.setVisibility(View.GONE);
                passBtn.setVisibility(View.VISIBLE);
                noBtn.setVisibility(View.VISIBLE);
                title_right_tv.setVisibility(View.VISIBLE);
                break;
            case TASK_STATUS_PASS:
                tipsBtn.setVisibility(View.VISIBLE);
                passBtn.setVisibility(View.GONE);
                noBtn.setVisibility(View.GONE);
                tipsBtn.setText("已通过");
                title_right_tv.setVisibility(View.GONE);
                break;
            case TASK_STATUS_FAIL:
                tipsBtn.setVisibility(View.VISIBLE);
                passBtn.setVisibility(View.GONE);
                noBtn.setVisibility(View.GONE);
                tipsBtn.setText("未通过");
                if (checkResult.getTaskOrder().getAuditDateTime() != null) {
                    if (System.currentTimeMillis() - checkResult.getTaskOrder().getAuditDateTime().getTime() >
                            1000 * 60 * 60 * 24) {
                        title_right_tv.setVisibility(View.GONE);
                    } else {
                        title_right_tv.setVisibility(View.VISIBLE);
                    }
                } else {
                    title_right_tv.setVisibility(View.VISIBLE);
                }
                break;
            default:
                tipsBtn.setVisibility(View.VISIBLE);
                passBtn.setVisibility(View.GONE);
                noBtn.setVisibility(View.GONE);
                tipsBtn.setText("未知状态");
                title_right_tv.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Tool.PictureSelectedSuccessSendMsg(requestCode, resultCode, data);
    }

    @OnClick(R.id.back)
    public void back() {
        finish();
    }

    @OnClick(R.id.pass_btn)
    void pass_btn() {
        presenter.succeedTheSameTask(checkResults.get(index).getTaskOrder().getId());

    }

    @OnClick(R.id.title_right_tv)
    void title_right_tv() {
        final TipsDialog tipsDialog = new TipsDialog(this);
        tipsDialog.setTitle("提示");
        CharSequence charSequence = ZxSpanTool.ColorSpan("*若存在恶意举报，严重者将被封号\n确认举报？", 17, 22, Color.RED);
        tipsDialog.setContent(charSequence);
        tipsDialog.setContentGravity(Gravity.CENTER);
        tipsDialog.setSubmitText("确定");
        tipsDialog.setOnClickListener(new TipsDialog.OnClickListener() {
            @Override
            public void onClick(int type) {
                tipsDialog.dismiss();
                if (type == TipsDialog.TYPE_SUBMIT) {
                    TipOffDialog tipOffDialog = new TipOffDialog(getActivity()
                            , TipOffDialog.REPORT_TYPE_SUBMIT, checkResults.get(index).getTaskOrder().getUserId(),
                            "" + checkResults.get(index).getTaskOrder().getId());
                    tipOffDialog.show();
                }

            }
        });
        tipsDialog.show();


    }

    @OnClick(R.id.right)
    void right() {
        if (checkResults == null) return;
        if (index < checkResults.size() - 1) {
            viewpager.setCurrentItem(++index, true);
        }
    }

    @OnClick(R.id.left)
    void left() {
        if (index > 0) {
            viewpager.setCurrentItem(--index, true);
        }
    }

    @OnClick(R.id.no_btn)
    void no_btn() {
        ImageTextDialog imageTextDialog = new ImageTextDialog(this);
        imageTextDialog.setTitle("不通过原因");
        imageTextDialog.setOnClickListener(new BaseDialog.OnClickListener() {
            @Override
            public void onClick(Dialog dialog, int button) {
                if (button == ImageTextDialog.BUTTON_CANCEL) {
                    dialog.dismiss();
                } else if (button == ImageTextDialog.BUTTON_SUBMIT) {
                    String editText = imageTextDialog.getContent();
                    if (TextUtils.isEmpty(editText)) {
                        ZxToast.showBottomToastLight("原因必须要填写哦！");
                        return;
                    }
//                    if (imageTextDialog.getImgs() == null || imageTextDialog.getImgs().size() < 1) {
//                        ZxToast.showBottomToastLight("必须要上传图片哦！");
//                        return;
//                    }
                    presenter.checkTask(checkResults.get(index).getTaskOrder().getId() + "",
                            true, editText, imageTextDialog.getImgs());
                    dialog.dismiss();
                }
            }
        });
        imageTextDialog.show();
//        final InputDialog inputDialog = new InputDialog(this);
//        inputDialog.setConfirmText("确定");
//        inputDialog.setTitleText("不通过原因");
//        inputDialog.setTipsText("");
//        inputDialog.getInputView().setHint("请输入不通过的原因");
//        inputDialog.getInputView().setInputType(InputType.TYPE_CLASS_TEXT); //数字类型
//        inputDialog.setOnClickListener(new InputDialog.OnClickListener() {
//            @Override
//            public void onClick(int button) {
//
//            }
//        });
//        inputDialog.show();
    }


    private void moveTask(int state) {
        CheckResult checkResult = checkResults.get(index);
        checkResult.getTaskOrder().setState(state);
        if (index + 1 < checkResults.size())
            viewpager.setCurrentItem(index + 1, true);
        EventBus.getDefault().post(new EventMsg(EventMsg.REQUEST_UPDATE_CHECK_LIST, null));
        EventBus.getDefault().post(new EventMsg(EventMsg.REQUEST_UPDATE_DATA_TASK_MANAGER_LIST_ITEM, null));
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        index = i;
        refresh();
        CheckEntity taskOrder = checkResults.get(i).getTaskOrder();
        presenter.userAllOrder(i, taskOrder.getTaskId(), taskOrder.getUserId(),taskOrder.getId());
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void onWait(String tips) {
        loadingDialog.setTipsText(tips);
        loadingDialog.show();
    }

    @Override
    public void onWait() {
        loadingDialog.setTipsText("请稍候...");
        loadingDialog.show();
    }

    @Override
    public void onTaskOrders(int index, List<TaskOrder> taskOrders) {
        adapter.putTaskOrder(index, taskOrders);
    }

    @Override
    public void onSuccess(Boolean data) {
        loadingDialog.cancel();
        moveTask(data ? TaskStatus.TASK_STATUS_FAIL.getStatus() : TaskStatus.TASK_STATUS_PASS.getStatus());
        refresh();
    }

    @Override
    public void onFailure(int code, String msg) {
        ZxToast.showFAILBottomToast(msg);
        loadingDialog.cancel();
    }

    @Override
    public void onPassRecordNum(int num) {
        loadingDialog.cancel();
        String tips = num > 0 ? "该用户曾完成过其它同类任务，请谨慎审核!\n确认通过？" : "确认通过？";
        TipsDialog tipsDialog = new TipsDialog(this);
        tipsDialog.setContent(tips);
        tipsDialog.setContentGravity(Gravity.CENTER);
        tipsDialog.setTitle("提示");
        tipsDialog.setSubmitText("确认");
        tipsDialog.setCancelText("我再看看");
        tipsDialog.setOnClickListener(type -> {
            tipsDialog.dismiss();
            if (type == TipsDialog.TYPE_SUBMIT) {
                presenter.checkTask(checkResults.get(index).getTaskOrder().getId() + "",
                        false, null, null);
            }
        });
        tipsDialog.show();
    }
}
